#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

from model import ClassM
from model import LectureM

class waitRoomPage(webapp.RequestHandler):
    def get(self):
        lectureKey = self.request.get("lectureKey")
        questionKey =self.request.get("questionKey")
        answer = self.request.get("answer")
        if lectureKey and answer:
            isQuizOn=ClassM.isQuizOn(lectureKey)
            if isQuizOn:
                viewVals={}
                question = ClassM.getQuestion(lectureKey)
                if question is None:
                    self.redirect("/student?lectureKey="+lectureKey)
                    return
                viewVals['question']= question
                viewVals['questionKey'] = questionKey
                viewVals['answer'] = ClassM.getRightAnswer(lectureKey)
                viewVals['isRight'] = ClassM.isCorrect(lectureKey, answer)
                viewVals['lecture']= LectureM.getLectureByKey(lectureKey)
                viewVals['lectureKey']= lectureKey
                self.response.out.write(template.render('view/postQuizWaitRoom.djhtml', viewVals))
            else:
                self.redirect("/student?lectureKey="+lectureKey)
        else:
            self.redirect("/page_not_found")#add explanation

application = webapp.WSGIApplication([(r'.*', waitRoomPage)],debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == '__main__':
    main()